﻿//********************************************************************************//
//创建日期：<创建日期，2016-09-18>
//创建作者：<孙雪婷，sunxueting@shanghai3h.com>
//功能说明：采灌计划 采灌计划输入
//********************************************************************************//

using System;
using System.Data;
using System.Web.UI;
using BLL;
using BLL.Access;
using DevExpress.Web;
using Entity;

namespace Web.Pages.QuShui.DiXiaS.JiHuaGL.CaiGuanJH
{
    public partial class CaiGuanJHSR : Page
    {
        #region 属性

        /// <summary>
        ///     保存结果集
        /// </summary>
        private DataTable dtSource
        {
            get { return ViewState["dtSource"] as DataTable; }
            set { ViewState["dtSource"] = value; }
        }

        #endregion

        #region 变量

        /// <summary>
        ///     年计划bll
        /// </summary>
        private QS_JH_NIANJIH _CGJH = new QS_JH_NIANJIH();

        /// <summary>
        ///     年计划bll
        /// </summary>
        private readonly QS_JH_NIANJIHBLL _CGJHBLL = new QS_JH_NIANJIHBLL();

        /// <summary>
        ///     存储过程执行类
        /// </summary>
        private readonly SpHelper _SpHelper = new SpHelper();

        /// <summary>
        ///     操作日志BLL
        /// </summary>
        private readonly SYS_LOGBLL _SYS_LOGBLL = new SYS_LOGBLL();

        /// <summary>
        ///     参数bll
        /// </summary>
        private readonly QS_CANSHUBBLL _CANSHUBLL = new QS_CANSHUBBLL();

        #endregion

        #region 事件

        /// <summary>
        ///     页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            //第一次页面加载
            if (!IsPostBack)
            {
                InitData();
                LoadData();
            }

            gvList.DataSource = dtSource;
            gvList.DataBind();
        }


        /// <summary>
        ///     年份切换事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void cmbNianF1_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        /// <summary>
        ///     响应前台回调
        /// </summary>
        protected void ASPxCallback1_Callback(object source, CallbackEventArgs e)
        {
            var id = Convert.ToInt32(hdCollection.Get("ID"));
            var msg = "";
            if (e.Parameter == "setinfo")
            {
                if (id != -1)
                {
                    var year = Convert.ToInt32(cmbNianF1.Value);
                    var dt_temp = _SpHelper.P_QSJH_JIHUASR(1, id, year).Tables[0];
                    var kcjh = dt_temp.Rows[0]["I_KAICAIJH"].ToString() != ""
                        ? Convert.ToDecimal(dt_temp.Rows[0]["I_KAICAIJH"]) / 10000
                        : 0;

                    var hgjh = dt_temp.Rows[0]["I_HUIGUANJH"].ToString() != ""
                        ? Convert.ToDecimal(dt_temp.Rows[0]["I_HUIGUANJH"]) / 10000
                        : 0;

                    //decimal hgjh =dt_temp.Rows[0]["I_HUIGUANJH"]).ToString() != "" ? Convert.ToDecimal(dt_temp.Rows[0]["I_HUIGUANJH"]) / 10000:0;
                    var dt = _CANSHUBLL.SelectQS_CANSHUBsDynamic(
                        "i_jlzt=0 and S_MINGCHENG=\'采灌比\' or S_MIAOSHU=\'采灌比\'", null);
                    var value = dt.Rows.Count > 0 ? dt.Rows[0]["S_ZHI"].ToString() : "1";
                    msg = string.Join("|", Convert.ToString(dt_temp.Rows[0]["S_YONGHUH"]),
                        Convert.ToString(dt_temp.Rows[0]["S_YONGHUM"]),
                        Convert.ToString(kcjh), Convert.ToString(hgjh), value);
                }

                e.Result = e.Parameter + "|0|" + msg;
            }
            else if (e.Parameter == "save")
            {
                var val = Convert.ToInt32(cmbShuRu.Value); //1 万立方米，2 立方米
                OperateState operateState;
                var y = Convert.ToInt32(cmbNianF1.Text);
                var yhh = txtBianHao.Text != "" ? txtBianHao.Text : "";
                var dt_jh = _CGJHBLL.SelectQS_JH_NIANJIHsDynamic(
                    string.Format(" i_jlzt=0 and i_y={0} and I_TYPE=1 and I_ZIDONGCJ = 0 and S_BIANHAO='{1}'", y, yhh),
                    null);
                if (dt_jh.Rows.Count > 0)
                {
                    id = Convert.ToInt32(dt_jh.Rows[0]["ID"]);
                    //修改操作
                    operateState = OperateState.Modify;
                    _CGJH = _CGJHBLL.SelectQS_JH_NIANJIH(id);
                    _CGJH.ID = id;
                }
                //添加
                else
                {
                    operateState = OperateState.Add;
                    _CGJH.ID = 0;
                    _CGJH.S_BIANHAO = yhh;
                    _CGJH.I_Y = y;
                    _CGJH.I_TYPE = 1;
                    _CGJH.I_ZIDONGCJ = 0;
                    _CGJH.S_TIANJIAR = LoginInfo.CurrentUser.UserID.ToString();
                    _CGJH.D_TIANJIARQ = BusinessTool.GetDataBaseDateTime();
                }

                var kcjh = txtKaiCaiJH.Text != "" ? Convert.ToDecimal(txtKaiCaiJH.Text) : 0;
                var hgjh = txtHuiGuanJH.Text != "" ? Convert.ToDecimal(txtHuiGuanJH.Text) : 0;

                _CGJH.I_KAICAIJH = val == 1 ? kcjh * 10000 : kcjh;
                _CGJH.I_HUIGUANJH = val == 1 ? hgjh * 10000 : hgjh;
                _CGJH.I_JLZT = 0;
                _CGJH.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
                _CGJH.D_CAOZUORQ = BusinessTool.GetDataBaseDateTime();
                _SYS_LOGBLL.Log(LoginInfo.CurrentUser, BusinessTool.WType.ToString(), "采灌计划输入", operateState, _CGJH,
                    null);
                var result = _CGJHBLL.InsertOrUpdate(_CGJH);
                if (result)
                {
                    var dt = _SpHelper.P_QSJH_JIHUASR(1, 0, y).Tables[0];
                    var kaicai = 0;
                    var huigua = 0;
                    for (var i = 0; i < dt.Rows.Count; i++)
                    {
                        kaicai += dt.Rows[i]["I_KAICAIJH"] != Convert.DBNull
                            ? Convert.ToInt32(dt.Rows[i]["I_KAICAIJH"])
                            : 0;
                        huigua += dt.Rows[i]["I_HUIGUANJH"] != Convert.DBNull
                            ? Convert.ToInt32(dt.Rows[i]["I_HUIGUANJH"])
                            : 0;
                    }

                    msg = "true" + "|" + kaicai + "|" + huigua;

                    e.Result = e.Parameter + "|0|" + msg;
                }
            }
            else if (e.Parameter == "huiguan")
            {
                var dt = _CANSHUBLL.SelectQS_CANSHUBsDynamic("i_jlzt=0 and S_MINGCHENG=\'采灌比\' or S_MIAOSHU=\'采灌比\'",
                    null);
                var value = dt.Rows.Count > 0 ? dt.Rows[0]["S_ZHI"].ToString() : "1";
                msg = value;
                e.Result = e.Parameter + "|0|" + msg;
            }
        }


        /// <summary>
        ///     客户端刷新list
        /// </summary>
        protected void gvList_CustomCallback(object sender, ASPxGridViewCustomCallbackEventArgs e)
        {
            var year = Convert.ToInt32(cmbNianF1.Value);
            dtSource = _SpHelper.P_QSJH_JIHUASR(1, 0, year).Tables[0];
            gvList.DataSource = dtSource;
            gvList.DataBind();
        }

        #endregion

        #region 方法

        /// <summary>
        ///     初始化数据
        /// </summary>
        private void InitData()
        {
            var year = DateTime.Now.Year;
            //for (int i = year - 10; i <= year; i++)
            //{
            //    cmbNianF1.Items.Add(i.ToString(), i);
            //}
            //cmbNianF1.SelectedIndex = 10;
            var dt = _SpHelper.P_QSJH_GETMAX_Y(2).Tables[0];
            var y = dt.Rows.Count > 0 ? Convert.ToInt32(dt.Rows[0][0]) : 0;
            if (y == 0)
                cmbNianF1.Items.Add(year.ToString(), year);
            else
                cmbNianF1.Items.Add((y + 1).ToString(), y + 1);
            cmbNianF1.SelectedIndex = 0;
        }

        /// <summary>
        ///     数据加载
        /// </summary>
        private void LoadData()
        {
            var year = Convert.ToInt32(cmbNianF1.Value);
            dtSource = _SpHelper.P_QSJH_JIHUASR(1, 0, year).Tables[0];
            gvList.DataSource = dtSource;
            gvList.DataBind();
            var kaicai = 0;
            var huigua = 0;
            for (var i = 0; i < dtSource.Rows.Count; i++)
            {
                kaicai += dtSource.Rows[i]["I_KAICAIJH"] != Convert.DBNull
                    ? Convert.ToInt32(dtSource.Rows[i]["I_KAICAIJH"])
                    : 0;
                huigua += dtSource.Rows[i]["I_HUIGUANJH"] != Convert.DBNull
                    ? Convert.ToInt32(dtSource.Rows[i]["I_HUIGUANJH"])
                    : 0;
            }

            lbKaiCai.Text = "开采计划合计" + kaicai + "立方米";
            lbHuiGuan.Text = "回灌计划合计" + huigua + "立方米";
            lbcount.Text = "共" + dtSource.Rows.Count + "条";
        }

        #endregion
    }
}